<?php
// -- 注册页面

/*
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(16) NOT NULL,
  `password` varchar(32) NOT NULL,
  `salt` varchar(8) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
*/

require_once "../init.php";

// 判断页面是否为POST提交
if ($_SERVER["REQUEST_METHOD"] == "POST") {
	// 获取参数
	$username = $_POST["username"];
	$password = $_POST["password"];
	// 随机密码盐,我这里临时随机数
	$salt = mt_rand(10000000, 99999999);

	// 加密密码
	$password = md5($password.$salt);

	// 生成用户数组数据
	$data = array(
		"username" => $username,
		"password" => $password,
		"salt" => $salt,
	);
	// $data = compact("username", "password", "salt");

	// 存入数据库
	insert("user", $data);

	// 注册成功 跳转到登陆页面
	redirect("login.php");
}
?>
<!DOCTYPE html>
<html>
<head>
	<title>注册</title>
</head>
<body>
<form method="post">
	用户名: <input type="text" name="username" /><br>
	密码: <input type="password" name="password" /><br>
	<input type="submit" value=" 注  册 " />
</form>
</body>
</html>